package com.jsy.order.controller;

import com.github.pagehelper.PageInfo;
import com.jsy.common.constant.StatusCode;
import com.jsy.common.vo.Result;
import com.jsy.order.pojo.Order;
import com.jsy.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/****
 * @Author:jsy
 * @Description:
 * @Date www.jsy.com
 *****/

@RestController
@RequestMapping("/order")
@CrossOrigin
public class OrderController {

    @Autowired
    private OrderService orderService;

    /***
     * Order分页条件搜索实现
     * @param order: 查询条件
     * @param page:当前页
     * @param size:每页显示多少条
     * @return 查询结果
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo<Order>> findPage(@RequestBody(required = false) Order order, @PathVariable  int page, @PathVariable  int size){
        //调用OrderService实现分页条件查询Order
        PageInfo<Order> pageInfo = orderService.findPage(order, page, size);
        return new Result<PageInfo<Order>>("查询成功！", pageInfo);
    }

    /***
     * Order分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return 查询结果
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo<Order>> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用OrderService实现分页查询Order
        PageInfo<Order> pageInfo = orderService.findPage(page, size);
        return new Result<PageInfo<Order>>("查询成功！", pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param order 查询条件
     * @return 查询结果
     */
    @PostMapping(value = "/search" )
    public Result<List<Order>> findList(@RequestBody(required = false) Order order){
        //调用OrderService实现条件查询Order
        List<Order> list = orderService.findList(order);
        return new Result<List<Order>>("查询成功！", list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id 主键
     * @return 查询结果
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable String id){
        //调用OrderService实现根据主键删除
        orderService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功！");
    }

    /***
     * 修改Order数据
     * @param order 更新对象
     * @return 操作结果
     */
    @PutMapping
    public Result update(@RequestBody Order order){
        //调用OrderService实现修改Order
        orderService.update(order);
        return new Result(true, StatusCode.OK, "修改成功！");
    }

    /***
     * 新增Order数据
     * @param order 新增对象
     * @return 操作结果
     */
    @PostMapping
    public Result<Order> add(@RequestBody Order order){
        Order result=null;
        try {
            //调用OrderService实现添加Order，需要添加用户名
            String username = SecurityContextHolder.getContext().getAuthentication().getName();
            //设置用户名
            order.setUsername(username);
            result = orderService.add(order);
            if (result!=null){
                return new Result<>("提交订单成功！",result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result<>("提交订单失败！",result);
    }

    /***
     * 根据ID查询Order数据
     * @param id 主键
     * @return 查询结果
     */
    @GetMapping("/{id}")
    public Result<Order> findById(@PathVariable String id){
        //调用OrderService实现根据主键查询Order
        Order order = orderService.findById(id);
        return new Result<Order>("查询成功！", order);
    }

    /***
     * 查询Order全部数据
     * @return 查询结果
     */
    @GetMapping
    public Result<List<Order>> findAll(){
        //调用OrderService实现查询所有Order
        List<Order> list = orderService.findAll();
        return new Result<List<Order>>("查询成功！", list);
    }
}
